//
//  Ugc_Detail_VC_WX.m
//  ZITHER
//
//  Created by weixin on 2018/9/11.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "Ugc_Detail_VC_WX.h"
#import "userUgcDetailModel.h"
#import "UgcCommentKewboardV.h"
#import "ReportView.h"
#import "ZYShareView.h"
#import <UShareUI/UShareUI.h>

@interface Ugc_Detail_VC_WX ()

@property (nonatomic,strong)QMUILabel * leb;

@property (nonatomic,strong)userUgcDetailModel * model;

@property (nonatomic,strong)UgcCommentKewboardV * keyboard;

@end

@implementation Ugc_Detail_VC_WX

- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self setNav];
    
    [self loaddata];
    
    [self setUI];
    
    @weakify(self);
    
    [RACObserve(self.keyboard, keyboardState)subscribeNext:^(id  _Nullable x) {
        
        @strongify(self);
        if ([x boolValue]) {
            self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES ;
        }else{
            self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO ;
        }
        
        self.tbv.userInteractionEnabled = [x boolValue];

    }];
 
}


-(void)setNav{
    
    [self.baseNavigationView NavigationBarStyle:1];
    self.baseNavigationView.leftButton1.hidden = YES;
    self.baseNavigationView.isShowShadow = YES;
    self.baseNavigationView.navViewStyle.title.text = @"详情";
    self.baseNavigationView.navViewStyle.rightBtn.hidden = NO;
    [self.baseNavigationView.navViewStyle.rightBtn setImage:[UIImage imageNamed:@"ugc _更多"] forState:UIControlStateNormal];
    
    CGRect f =self.baseNavigationView.navViewStyle.rightBtn.frame;
    f.size.width = 50;
    f.size.height = 30;
    f.origin.y = f.origin.y-10;
    f.origin.x = f.origin.x-35;
    self.baseNavigationView.navViewStyle.rightBtn.frame =f;
    self.baseNavigationView.navViewStyle.rightBtn.centerY = self.baseNavigationView.navViewStyle.centerY+centerYH;

    @weakify(self);
    [[self.baseNavigationView.navViewStyle.rightBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self showZYShare];
    }];
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
//    [self.baseNavigationView setBackgroundColor:[UIColor clearColor]];
//    self.baseNavigationView.bottomLineView.backgroundColor = [UIColor clearColor];
//    self.baseNavigationView.leftButton1.hidden = YES;
//
//    /* TODO: ================= 导航栏箭头颜色 =================*/
//    QMUIButton *btn = [[QMUIButton alloc]initWithFrame:CGRectMake(14,kStatusBarHeight+5,40,40)];
//    [btn setImage:[UIImage imageNamed:@"common_back"] forState:UIControlStateNormal];
//    [self.view addSubview:btn];
//    @weakify(self);
//    [[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        @strongify(self);
//        [self.navigationController popViewControllerAnimated:YES];
//    }];
//
//    [self.view addSubview:self.leb];
//    self.tabBarController.tabBar.hidden = YES;

}

-(void)loaddata{
    @weakify(self);
    
    [[serviceDefualt loadGetUserUgcDetail:self.ugcId reponseObjctClass:[userUgcDetailModel class]]subscribeNext:^(ResponseData * x) {
        @strongify(self);
        if (x.success) {
            self.model = x.responseData;
            [self.tbv reloadData];
        }else{
            [Tools showTips:x.errorInfo];
        }
    }];
}

-(void)setUI{
    
    [self.view addSubview:self.tbv];
    [self.view addSubview:self.keyboard];

    [self.tbv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(NaviHeight);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.keyboard.mas_top).offset(0);
    }];
    
}


-(void)setModel:(userUgcDetailModel *)model{
    _model = model;
    self.tbv.model = model;
}

-(void)setUgcId:(NSString *)ugcId{
    _ugcId = ugcId;
    self.tbv.ugcId = ugcId;
    
}

-(QMUILabel *)leb{
    if (!_leb) {
        _leb = [[QMUILabel alloc]initWithFrame:CGRectMake(20,kStatusBarHeight+50, 200, 22)];
        _leb.text = @"详情";
        _leb.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
        _leb.textColor = [UIColor blackColor];
    }
    return _leb;
}


-(Ugc_Detail_TBV_WX *)tbv{
    if (!_tbv) {
        _tbv = [[Ugc_Detail_TBV_WX alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
      
        @weakify(self);
        [_tbv setKeyboard:^(NSString * _Nonnull nickName, NSString * _Nonnull relId, NSString * _Nonnull replyUserId, NSString * _Nonnull relType, NSString * _Nonnull commentParentId, NSString * _Nonnull commentMasterId, NSInteger type,NSInteger indexPath) {
            @strongify(self);
            self.keyboard.relId = relId;
            self.keyboard.replyUserId = replyUserId;
            self.keyboard.relType = relType;
            self.keyboard.commentParentId = commentParentId;
            self.keyboard.commentMasterId = commentMasterId;
            self.keyboard.type = type;
            self.keyboard.indexPath = indexPath;

            if (self.keyboard.keyboardState) {
                [self.keyboard.textView becomeFirstResponder];
                self.keyboard.Reply.hidden = NO;
            }
            NSLog(@"%@",nickName);
            self.keyboard.Reply.text = [NSString stringWithFormat:@"回复: %@",nickName];
        }];
     
        [_tbv setLikeSuccess:^{
            @strongify(self);
           
            [[serviceDefualt loadGetUserUgcDetail:self.ugcId reponseObjctClass:[userUgcDetailModel class]]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                    self.model = x.responseData;
                    NSArray *indexPaths = @[[NSIndexPath indexPathForRow:0 inSection:1]];
                    [self.tbv reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
                    [self loaddata];
                }else{
                    [Tools showTips:x.errorInfo];
                }
            }];
       
        }];
    }
    return _tbv;
}

-(UgcCommentKewboardV *)keyboard{
    if (!_keyboard) {
        _keyboard = [[UgcCommentKewboardV alloc]initWithFrame:CGRectMake(0, kScreenH-49-kBottomSafeHeight, kScreenW, 49)];
        @weakify(self);
        _keyboard.backgroundColor = [UIColor whiteColor];
        [_keyboard setPlaceholderText:@" 说点什么"];
        _keyboard.EwenTextViewBlock = ^(NSString *test){
            @strongify(self);
            [[serviceDefualt loadAddUserComment:self.ugcId commentContent:test relType:@"1" replyUserId:self.model.userUgc.userId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {

                if (x.success) {
                    [Tools showTips:@"评论成功"];
//                    self.tbv.page = 0;
//                    [self.tbv setLoad];
//                    [self loaddata];
                    UgcCommentListModelSub * modelComment = [UgcCommentListModelSub yy_modelWithDictionary:x.responseRawData[@"data"]];
                    [self addMainComment:modelComment index:0];

                }else{
                    if (![x.errorInfo isEqualToString:@"未登录"]) {
                        [Tools showTips:x.errorInfo];
                    }else{
//                        [Tools pushSinge];
                    }
                    
                }
            }];
        };
        
        [_keyboard setSendSubComment:^(UgcCommentListModelSub * _Nonnull model) {
            @strongify(self);
//            [self addComment:model index:self.keyboard.indexPath];
//            NSLog(@"%ld",self.tbv.model.goodsList.count);
//            NSInteger index = self.keyboard.indexPath-self.tbv.model.goodsList.count-2;
            
            [self addSubComment:model index:self.keyboard.indexPath-2];
        }];
        
        
//        [_keyboard setSendSubComment:^{
//            @strongify(self);
//            self.tbv.page = 0;
//            [self.tbv setLoad];
//            [self loaddata];
//        }];
        
    }
    return _keyboard;
}

-(void)addSubComment:(UgcCommentListModelSub*)model index:(NSInteger)index{
    //1+1+1+self.model.goodsList.count
    //添加数据源
    // 6
    if (self.tbv.arrMComment.count==0) {
//        [self.tbv.arrMComment addObject:model];
        self.tbv.page = 0;
        [self.tbv setLoad];
        [self loaddata];
    }else{
        [self.tbv.arrMComment insertObject:model atIndex:index+3];
        [self.tbv insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index+3 inSection:0]]
                        withRowAnimation:UITableViewRowAnimationFade];
    }
//    [self.tbv.arrMComment insertObject:model atIndex:index+3];
//    NSInteger index1=index+1+1+1+self.Tbv.model.goodsList.count;
//    NSLog(@"index1 = %ld",index1+1);
    //    [self.Tbv beginUpdates];

    //    [self.Tbv endUpdates];
}
//添加评论 type: 0:为主评论插入第一行 1:主评论第一行 2:子评论下一行 type:(NSInteger)type
-(void)addMainComment:(UgcCommentListModelSub*)model index:(NSInteger)index{
    //1+1+1+self.model.goodsList.count
    //添加数据源
    if (self.tbv.arrMComment.count==0) {
//        [self.tbv.arrMComment addObject:model];
//        [self.tbv insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]
//                        withRowAnimation:UITableViewRowAnimationFade];
        self.tbv.page = 0;
        [self.tbv setLoad];
        [self loaddata];
    }else{
//        if (self.tbv.arrMComment.count <index) {
//            [self.tbv.arrMComment insertObject:model atIndex:index+2];
//            [self.tbv insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index+2 inSection:0]]
//                            withRowAnimation:UITableViewRowAnimationFade];
//        }else{
        NSLog(@"index %ld",index);
            [self.tbv.arrMComment insertObject:model atIndex:index];
            [self.tbv insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]
                            withRowAnimation:UITableViewRowAnimationFade];
//        }

    }
//    NSInteger index1=index+1+1+1+self.Tbv.model.goodsList.count;
//    NSLog(@"index1 = %ld",index1+1);
    //    [self.Tbv beginUpdates];

    //    [self.Tbv endUpdates];
}


-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}



-(void)showZYShare{
    
    //    __weak typeof(self) weakSelf = self;
    
    // 创建代表每个按钮的模型
//    ZYShareItem *item0 = [ZYShareItem itemWithTitle:@"分享朋友圈"
//                                               icon:@"分享_朋友圈"
//                                            handler:^{[self itemAction:@"2"];}];
//    
//    ZYShareItem *item1 = [ZYShareItem itemWithTitle:@"分享微信"
//                                               icon:@"分享_微信"
//                                            handler:^{ [self itemAction:@"1"]; }];
//    
//    ZYShareItem *item2 = [ZYShareItem itemWithTitle:@"分享QQ"
//                                               icon:@"分享_qq"
//                                            handler:^{ [self itemAction:@"4"]; }];
//    
//    ZYShareItem *item3 = [ZYShareItem itemWithTitle:@"分享QQ空间"
//                                               icon:@"分享_QQ空间"
//                                            handler:^{ [self itemAction:@"5"]; }];
//    
//    ZYShareItem *item4 = [ZYShareItem itemWithTitle:@"分享微博"
//                                               icon:@"分享_微博"
//                                            handler:^{ [self itemAction:@"0"]; }];
//    
//    ZYShareItem *itemCustom1 = [ZYShareItem itemWithTitle:@"分享链接"
//                                                     icon:@"分享_链接"
//                                                  handler:^{ [self itemAction:@"1024"]; }];
//    
//    ZYShareItem *itemCustom2 = [ZYShareItem itemWithTitle:@"更多举报"
//                                                     icon:@"更多_举报"
//                                                  handler:^{ [self itemAction:@"1025"]; }];
//    
//    
//    NSArray *shareItemsArray = @[item0, item1, item2, item3, item4];
//    NSArray *functionItemsArray = @[itemCustom1, itemCustom2];
    
    
//
    NSMutableArray *shareItemsArray = [[NSMutableArray alloc]init];
    NSMutableArray *functionItemsArray = [[NSMutableArray alloc]init];
    NSArray *shareItemsTitleArray = @[@"分享朋友圈",@"分享微信",@"分享QQ",@"分享QQ空间",@"分享微博"];
    NSArray *shareItemsTitle2Array = @[@"分享_朋友圈",@"分享_微信",@"分享_qq",@"分享_QQ空间",@"分享_微博"];
    
    
    
    
    NSArray *shareItemsTitle3Array = @[@"2",@"1",@"4",@"5",@"0"];
    NSArray *functionItems1Array = @[@"分享链接",@"举报",@"屏蔽用户"];
    NSArray *functionItems2Array = @[@"分享_链接",@"更多_举报",@"用户_屏蔽"];
    NSArray *functionItems3Array = @[@"1024",@"1025",@"1026"];
    
    if ([kUSER_INFO_MANAGER.userId isEqualToString:self.userID]) {
        functionItems1Array = @[@"分享链接",@"举报"];
        functionItems2Array = @[@"分享_链接",@"更多_举报"];
        functionItems3Array = @[@"1024",@"1025"];
    }
 
    for (int i = 0; i<shareItemsTitleArray.count; i++) {
        if ([[UMSocialManager defaultManager]isInstall:i]) {
            ZYShareItem *item = [ZYShareItem itemWithTitle:shareItemsTitleArray[i]
                                                      icon:shareItemsTitle2Array[i]
                                                   handler:^{[self itemAction:shareItemsTitle3Array[i]];}];
            [shareItemsArray addObject:item];
        }
    }
    
    for (int i = 0; i<functionItems1Array.count; i++) {
        ZYShareItem *itemCustom1 = [ZYShareItem itemWithTitle:functionItems1Array[i]
                                                         icon:functionItems2Array[i]
                                                      handler:^{ [self itemAction:functionItems3Array[i]];
                                                      }];
        [functionItemsArray addObject:itemCustom1];
    }
    
    // 创建shareView
    ZYShareView *shareView = [ZYShareView shareViewWithShareItems:shareItemsArray
                                                    functionItems:functionItemsArray];
    // 弹出shareView
    [shareView show];
    
}


#pragma mark -

- (void)itemAction:(NSString *)title
{
    NSLog(@"%@", title);
    NSInteger index=  [title integerValue];
    if (index == 1024) {
        UIPasteboard * pastboard = [UIPasteboard generalPasteboard];
        pastboard.string = [NSString stringWithFormat:@"%@/#/share/ugc/%@",RootUrlWeb,self.ugcId];

        [Tools showTips:@"复制链接成功"];
    }else if (index == 1025){
        if (!kUSER_INFO_MANAGER.userIsLogin) {
            [Tools pushSinge];
        }else{
            ReportView *rep = [[ReportView alloc]initWithFrame:CGRectMake(0,0, kScreenW, kScreenH)];
            rep.typeId = self.ugcId;
            rep.state = ReportViewStateUgc;
            [rep show];
        }
        
    }else if (index == 1026){
        [[AccountService loadUserRepelAdd:self.model.userUgc.userId reponseObjctClass:nil]subscribeNext:^(ResponseData* x) {
            if (x.success) {
                [Tools showTips:@"已屏蔽"];
            }else{
                [Tools showTips:x.errorInfo];
            }
        }];
    }else{
        if (!kUSER_INFO_MANAGER.userIsLogin) {
            [Tools pushSinge];
        }else{
            [self shareWebPageToPlatformType:index];
        }
    }
    
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    NSString *name = [NSString stringWithFormat:@"%@ 的分享",kUSER_INFO_MANAGER.nickName];
    //创建网页内容对象
    
    UIImage * image = [UIImage imageNamed:@"icon"];
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:name descr:self.model.userUgc.ugcContent thumImage:[UIImage imageNamed:@"icon"]];
    //设置网页地址

    shareObject.webpageUrl = [NSString stringWithFormat:@"%@/#/share/ugc/%@",RootUrlWeb,self.ugcId];

    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
    
}
@end
